package com.yidu.best.basicSetup.exchange.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.best.basicSetup.exchange.dao.ExchangeDao;
import com.yidu.best.basicSetup.exchange.domain.Exchange;
import com.yidu.best.basicSetup.exchange.domain.ExchangeVo;
import com.yidu.best.basicSetup.exchange.service.ExchangeService;
import com.yidu.best.basicSetup.exchange.util.IDUtil;

@Service
public class ExchangeServiceImpl implements ExchangeService {
	@Autowired
	private ExchangeDao exchangeDao;

	@Override
	public Map<String, Object> findExchange(ExchangeVo exchangeVo) {
		// 调用dao层方法查询数量
		int count = exchangeDao.findCountExchange(exchangeVo);
		// 计算页码
		int page = (exchangeVo.getPage() - 1) * exchangeVo.getLimit();
		exchangeVo.setPage(page);
		// 调用dao层方法分页查询
		List<Exchange> exchangeList = exchangeDao.findExchange(exchangeVo);
		Map<String, Object> map = new HashMap<String, Object>();
		// 将数据存入map中
		map.put("code", 0);
		map.put("count", count);
		map.put("data", exchangeList);
		return map;
	}

	@Override
	public int addExchange(Exchange exchange) {
		// 随机一个16位的ID
		exchange.setExchangeId(IDUtil.getUUID());
		List<Exchange> exchangeList = exchangeDao.findAll();
		String exchangeName = null;
		for (Exchange exchange2 : exchangeList) {
			exchangeName = exchange2.getExchangeName();
		}
		if (exchangeName.equals(exchange.getExchangeName())) {
			return 0;
		} else {
			// 调用业务层修改方法
			int rs = exchangeDao.addExchange(exchange);
			// 如何rs=1 则修改成功
			return rs;

		}
	}

	@Override
	public int updateExchange(Exchange exchange) {
		List<Exchange> exchangeList = exchangeDao.findAll();
		String exchangeName = null;
		for (Exchange exchange2 : exchangeList) {
			exchangeName = exchange2.getExchangeName();
		}
		if (exchangeName.equals(exchange.getExchangeName())) {
			return 0;
		} else {
			// 调用业务层修改方法
			int rs = exchangeDao.updateExchange(exchange);
			return rs;

		}
	}

	@Override
	public int deleteExchange(String exchangeIds) {
		// 定义变量
		int rs = 0;
		// 定义数组接受前台返回的字符串数组 将,去掉
		String[] exchangeId = exchangeIds.split(",");
		// 将接收的数组遍历
		for (int i = 0; i < exchangeId.length; i++) {
			// 调用业务层方法
			rs = exchangeDao.deleteExchange(exchangeId[i]);
		}
		return rs;
	}

	@Override
	public List<Exchange> findAll() {
		return exchangeDao.findAll();
	}

}
